{include file="../application/admin/view/header.html" /}
</head>
<body>
<div class="container">
	<div class="col-md-12">
		<div class="alert kesn_prompt"></div>
	</div>
	<div class="row input-group" style="display: none;">
	  <span class="input-group-addon">id</span>
	  <input type="text" class="form-control" placeholder="" name="course_module_id" {if condition="$action eq 'edit'"} value="{$course_module.id}" {/if} />
	</div>
	<div class="row input-group">
	  <span class="input-group-addon">教程模块名</span>
	  <input type="text" class="form-control" placeholder="" name="course_module" {if condition="$action eq 'edit'"} value="{$course_module.course_module}" {/if} />
	</div>
	&nbsp;&nbsp;<br/>
	<div class="row">
		<button class="btn btn-info add_course">新增教程<span class="fui-plus btn-tip"></span></button>
		{if condition="$action eq 'edit'"}
		<form class="navbar-form navbar-right" action="save_course_module_view" role="search"  method="get">
			<input type="number" name="course_module_id" value="{$course_module.id}" style="display: none;">
		  <div class="form-group">
		    <div class="input-group input-group-rounded">
		      <input class="form-control" id="keyword" name="keyword" type="search" placeholder="请输入教程名">
		      <span class="input-group-btn">
		        <button type="submit" class="btn kesn_search"><span class="fui-search"></span></button>
		      </span>
		    </div>
		  </div>
		</form>
		{/if}
	</div>


	
	
	<div></div>
	<div class="row">
		<table class="table table-hover">
			<thead>
				<th>ID</th>
				<th>教程名</th>
				<th>操作</th>
			</thead>
			<tbody>
			{if condition="$action eq 'edit'"}
			{volist name="course" id="course"}
			<tr class="_item">
					<th scrope="row">{$course.id}</th>
					<td><input type="text" class="form-control input-sm" placeholder="" name="course_name" value="{$course.course}" /></td>
					<td><div>
						<a href="#" class="btn btn-primary kesn_to btn-sm" data-id="edit_course" data-title="编辑教程" data-url="/index.php/admin/course/save_course_view?course_id={$course.id}">编辑<span class="fui-new btn-tip"></span></a>
						<a href="#" class="btn btn-danger kesn_del btn-sm" data-_id={$course.id} data-_name="{$course.course}">删除<span class="fui-cross btn-tip"></span></a>
					</div></td>
			</tr>
			{/volist}
			{/if}
		</tbody>
		</table>
		{if condition="$action eq 'edit'"}
		{$page}
		{/if}
	</div>
	<button class="btn btn-info save_course_module">绑定教程模块</button>
</div>

{include file="../application/admin/view/footer.html" /}
<script type="text/javascript">
$('.add_course').click(function(){
	var _html = '<tr class="_item"><th scrope="row"></th><td><input type="text" class="form-control input-sm" placeholder="" name="course_name"/></td><td class="change_course_button"><div><a href="#" class="btn btn-info sure_course btn-sm">确认<span class="fui-check-circle btn-tip"></span></a><a href="#" class="btn btn-default cancle_course btn-sm">取消<span class="fui-trash btn-tip"></span></a></div></td></tr>';
	$('tbody').append(_html);
});

$('tbody').on('click','.kesn_del',function(){
	var _this = $(this);
	var _id = _this.attr('data-_id');
	var _name = _this.attr('data-_name');
	var _url = 'del_course';
	var message = '您确实要删除 '+_id+': '+_name+' 吗？';
	kesn_del(_id,_url,message,_this);

});

//取消
$('tbody').on('click','.cancle_course',function(){
	var _this = $(this);
	_this.parents('._item').remove();
});
//确认
$('tbody').on('click','.sure_course',function(){
	var _this = $(this);
	var this_i = _this.parents('._item');
	var course_name = this_i.find('input[name=course_name]').val();
	if (course_name == '') {
		kesn_prompt('fail','教程名不能为空！');
		return;
	}
	$.ajax({
			url :'add_course',
			async:true,
			type:'POST',
			data:{
				'course_name' :course_name,
			},
			dataType:'JSON',
			success:function(data,textStatus,jqXHR){
    				if (data.err != 0) {
    					kesn_prompt('fail',data.content);
    				}else if (data.err == 0) { 
                    	var _html = '<div><a href="#" class="btn btn-primary kesn_to btn-sm" data-id="edit_course" data-title="编辑课程">编辑<span class="fui-new btn-tip"></span></a><a href="#" class="btn btn-danger kesn_del btn-sm" data-_id="'+data.course_id+'" data-_name="'+course_name+'">删除<span class="fui-cross btn-tip"></span></a></div>';
                    	this_i.find('.change_course_button').html(_html);
                    	this_i.find('th[scrope=row]').html(data.course_id);
                    	this_i.find('a.kesn_to').attr('data-url','/index.php/admin/course/save_course_view?course_id='+data.course_id);
                    }

    			},
    			error:function(){
    				kesn_prompt('fail','网络出错',function(){});
    			}
		});
});
//新增或保存教程
$('.save_course_module').click(function(){
	var course_module = {};
	course_module['id'] = $('input[name=course_module_id]').val();
	course_module['course_module'] = $('input[name=course_module]').val();
	course_module['course'] = new Array();
	$('._item').each(function(){
		var _this = $(this);
		var _id = _this.find('th[scrope=row]').html();
		var _course = _this.find('input[name=course_name]').val();
		if (_course == '') {
			kesn_prompt('fail','教程名不能为空！');
			return;
		}
		if (_id != '') {
			var course_tmp = {};
			course_tmp['course'] = _course;
			course_tmp['id'] = _id;
			course_module['course'].push(course_tmp);
		}
	});
	$.ajax({
			url : 'save_course_module',
			async:true,
			type:'POST',
			data:course_module,
			dataType:'JSON',
			success:function(data,textStatus,jqXHR){
    				console.log(data);
    				if (data.err != 0) {
    					kesn_prompt('fail',data.content);
    				}else if (data.err == 0) {
                        kesn_prompt('success',data.content,function(){
                        	$('input[name=course_module_id]').val(data.course_module_id);
                        }); 
                    }

    			},
    			error:function(){
    				kesn_prompt('fail','网络出错');
    			}
		});
});
</script>
</footer>
</body>
</html>